package com.lengmou.service.impl;

import com.lengmou.dao.OrdersDao;
import com.lengmou.entity.Orders;
import com.lengmou.service.OrdersService;
import com.lengmou.util.TimeTool;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("OrdersService")
public class OrdersServiceImpl implements OrdersService {
    @Resource
    private OrdersDao od;
    @Override
    public List<Orders> getAll() {
        return od.querysAll();
    }

    @Override
    public List<Orders> getOrdersByUserid(int userid) {
        return od.querysByUserId(userid);
    }

    @Override
    public List<Orders> getOrdersByUseridState(int userid,int state) {
        List<Orders> ordersList = new ArrayList<>();
        List<Orders> ordersByUserid = getOrdersByUserid(userid);
        for (Orders o:ordersByUserid) {
            if(o.getState()==state){
                ordersList.add(o);
            }
        }
        return ordersList;
    }

    @Override
    public int addOrders(Orders orders) {
        if (orders.getCreatetime()==null){
            orders.setCreatetime(TimeTool.time());
        }
        return od.addOrders(orders);
    }

    @Override
    public Orders getOrdersById(int id) {
        return od.queryById(id);
    }

    @Override
    public int setOrdersUserid(Orders orders) {
        Orders ordersById = getOrdersById(orders.getId());
        if(ordersById!=null){
            ordersById.setUserid(orders.getUserid());
            return od.updateOrders(ordersById);
        }
        return -1;
    }

    @Override
    public int setOrdersCreateTime(Orders orders) {
        Orders ordersById = getOrdersById(orders.getId());
        if(ordersById!=null){
            ordersById.setCreatetime(orders.getCreatetime());
            return od.updateOrders(ordersById);
        }
        return -1;
    }

    @Override
    public int setOrdersPayTime(Orders orders) {
        Orders ordersById = getOrdersById(orders.getId());
        if(ordersById!=null){
            ordersById.setPaytime(orders.getPaytime());
            return od.updateOrders(ordersById);
        }
        return -1;
    }

    @Override
    public int setOrdersRemarks(Orders orders) {
        Orders ordersById = getOrdersById(orders.getId());
        if(ordersById!=null){
            ordersById.setRemarks(orders.getRemarks());
            return od.updateOrders(ordersById);
        }
        return -1;
    }

    @Override
    public int setOrdersState(Orders orders) {
        Orders ordersById = getOrdersById(orders.getId());
        if(ordersById!=null){
            ordersById.setState(orders.getState());
            return od.updateOrders(ordersById);
        }
        return -1;
    }

    @Override
    public int deleteOrders(int id) {
        Orders ordersById = getOrdersById(id);
        if(ordersById!=null){
            return od.deleteOrders(id);
        }
        return -1;
    }

    @Override
    public int updateOrders(Orders orders) {

        return od.updateOrders(orders);
    }

    @Override
    public List<Orders> search(Map<String, Object> map) {
        return od.search(map);
    }
}
